package org.dromara.system.controller.system;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.bo.SysDictTypeBo;
import org.dromara.system.domain.vo.SysDictTypeVo;
import org.dromara.system.entity.Dish;
import org.dromara.system.entity.DishDto;
import org.dromara.system.entity.PageTwoParams;
import org.dromara.system.service.DishService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 缓存监控
 *
 * @author Lion Li
 */
//@SaCheckPermission("monitor:cache:list")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/test2")
public class DishController {
//    private final RedissonConnectionFactory connectionFactory;
    private final DishService dishService;
    @GetMapping("pageInfo")
    public R page(@ParameterObject PageTwoParams params, String name){
        return R.ok(dishService.pageLists(params,name));
    }
    /**
     * 获取缓存监控列表
     */
//    @SaCheckPermission("monitor:cache:list")
//    @GetMapping()
//    public R<CacheListInfoVo> getInfo() throws Exception {
//        RedisConnection connection = connectionFactory.getConnection();
//        Properties commandStats = connection.commands().info("commandstats");
//
//        List<Map<String, String>> pieList = new ArrayList<>();
//        if (commandStats != null) {
//            commandStats.stringPropertyNames().forEach(key -> {
//                Map<String, String> data = new HashMap<>(2);
//                String property = commandStats.getProperty(key);
//                data.put("name", StringUtils.removeStart(key, "cmdstat_"));
//                data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
//                pieList.add(data);
//            });
//        }
//        CacheListInfoVo infoVo = new CacheListInfoVo();
//        infoVo.setInfo(connection.commands().info());
//        infoVo.setDbSize(connection.commands().dbSize());
//        infoVo.setCommandStats(pieList);
//        return R.ok(infoVo);
//    }
    @PostMapping("add")
    @Operation(summary = "添加")
    public R update(@RequestBody DishDto dishDto) {
        dishService.add(dishDto);
        return R.ok("新增成功");
    }
    @PostMapping("change")
    @Operation(summary = "修改")
    public R change(@RequestBody DishDto dishDto) {
        dishService.update(dishDto);
        return R.ok("新增成功");
    }
//    @GetMapping("/{id}")
//    @Operation(summary = "根据id查询信息")
//    public R getInfoById(@PathVariable Long id){
//        DishDto dishDto = dishService.getByIdWithFlavor(id);
//        return R.ok(dishDto);
//    }
@GetMapping("/{id}")
@Operation(summary = "删除")
public R remove(@PathVariable Long id){
    dishService.removeById(id);
    return R.ok("删除成功");
}

    @PostMapping("/export")
    public void export(Dish dish, HttpServletResponse response) {
       List<Dish> list = dishService.list();
        ExcelUtil.exportExcel(list, "字典类型", Dish.class, response);
    }

}
